package model

import (
	"github.com/hwcer/cosgo/random"
	"server/share/config"
	"time"
)

// 流星
type RoleStar struct {
	List []int32 `json:"starList" bson:"list"`
	Time int64   `json:"lastGetTime" bson:"time"` //MS
}

type RoleStarBuff struct {
	Buff    []RoleStarBuffItem    `json:"buff" bson:"buff"`
	Item    []RoleStarBuffItem    `json:"itemBuff" bson:"item"`
	Monster []RoleStarBuffMonster `json:"monster" bson:"monster"`
}

type RoleStarBuffItem struct {
	Val       string `json:"val" bson:"val"`
	IfPercent int32  `json:"ifPercent" bson:"ifPercent"`
	Field     string `json:"field" bson:"field"`
	RandomVal int32  `json:"randomVal" bson:"randomVal"`
	BeginTime int64  `json:"beginTime" bson:"beginTime"`
	EndTime   int64  `json:"endTime" bson:"endTime"`
}

type RoleStarBuffMonster struct {
	WaveID int32                       `json:"waveID" bson:"waveID"`
	Mid    int32                       `json:"mid" bson:"mid"`
	Reward []RoleStarBuffMonsterReward `json:"reward" bson:"reward"`
}

type RoleStarBuffMonsterReward struct {
	Id  int32 `json:"id" bson:"id"`
	Num int32 `json:"num" bson:"num"`
	Key int32 `json:"key" bson:"key"`
}

func (this *Role) GetStar(now ...time.Time) (*RoleStar, bool) {
	var t time.Time
	if len(now) > 0 {
		t = now[0]
	} else {
		t = time.Now()
	}
	if this.Star == nil {
		return &RoleStar{Time: t.UnixMilli()}, false
	}
	r := *this.Star
	r.List = append([]int32{}, this.Star.List...)
	meteorMaxNumConfig := config.Data.Base["meteorMaxNum"]
	meteorRefreshTimeConfig := config.Data.Base["meteorRefreshTime"]
	if meteorMaxNumConfig == nil || meteorRefreshTimeConfig == nil {
		return &r, false
	}
	if meteorMaxNumConfig.Val > 0 && len(r.List) >= int(meteorMaxNumConfig.Val) {
		r.Time = t.UnixMilli()
		return &r, false
	}

	speed := int64(meteorRefreshTimeConfig.Val * 1000)
	sub := t.UnixMilli() - r.Time
	num := sub / speed
	if num <= 0 {
		return &r, false
	}
	tot := len(r.List) + int(num)
	if tot >= int(meteorMaxNumConfig.Val) {
		r.Time = t.UnixMilli()
		num = int64(meteorMaxNumConfig.Val) - int64(len(r.List))
	} else {
		r.Time += num * speed
	}
	rdm := random.Random{}
	for k, v := range config.Data.MeteorGroup {
		if this.BattlePoint >= v.EffectStage {
			rdm.Add(k, v.Val)
		}
	}

	for i := int64(0); i < num; i++ {
		if k := rdm.Roll(); k > 0 {
			r.List = append(r.List, k)
		}
	}

	return &r, true
}

func (this *Role) GetStarBuff(now ...time.Time) (*RoleStarBuff, bool) {
	if this.StarBuff == nil {
		return &RoleStarBuff{}, false
	}
	r := *this.StarBuff
	r.Buff = append([]RoleStarBuffItem{}, this.StarBuff.Buff...)
	r.Item = append([]RoleStarBuffItem{}, this.StarBuff.Item...)
	r.Monster = append([]RoleStarBuffMonster{}, this.StarBuff.Monster...)
	//检查过期数据
	return &r, false

}
